import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "@/layouts/index.vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

/**
 * @description 路由配置
 */
export const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
    meta: { title: "登录", hidden: true },
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        meta: { title: "首页", icon: "HomeFilled", affix: true },
      },
    ],
  },
  // 漏洞管理
  {
    path: "/vulnerability",
    component: Layout,
    redirect: "/vulnerability/list",
    meta: { title: "漏洞管理", icon: "SecurityScan" },
    children: [
      {
        path: "list",
        name: "VulnerabilityList",
        component: () => import("@/views/vulnerability/List.vue"),
        meta: { title: "漏洞资源列表", icon: "Document" },
      },
    ],
  },
  // 漏洞匹配
  // {
  //   path: "/matching",
  //   component: Layout,
  //   redirect: "/matching/auto",
  //   meta: { title: "漏洞匹配", icon: "Connection" },
  //   children: [
  //     {
  //       path: "auto",
  //       name: "AutoMatching",
  //       component: () => import("@/views/matching/auto/index.vue"),
  //       meta: { title: "漏洞自动匹配", icon: "Lightning" },
  //     },
  //   ]
  // },
  {
    path: "/detection",
    component: Layout,
    redirect: "/detection/vuln",
    meta: { title: "漏洞检测", icon: "Connection" },
    children: [
      {
        path: "vuln",
        name: "VulnDetection",
        component: () => import("@/views/matching/detection/index.vue"),
        meta: { title: "漏洞检测", icon: "Monitor" },
      },
    ],
  },
  // 漏洞分析管理
  {
    path: "/analysis",
    component: Layout,
    redirect: "/analysis/root-cause",
    meta: { title: "漏洞分析管理", icon: "DataAnalysis" },
    children: [
      {
        path: "root-cause",
        name: "RootCauseAnalysis",
        component: () => import("@/views/analysis/RootCause.vue"),
        meta: { title: "漏洞成因分析", icon: "Search" },
      },
      // {
      //   path: "root-cause1",
      //   name: "RootCauseAnalysis1",
      //   component: () => import("@/views/analysis/RootCause1.vue"),
      //   meta: { title: "漏洞成因分析1", icon: "Search" },
      // },
    ],
  },
  // 漏洞情报收录
  {
    path: "/intelligence",
    component: Layout,
    redirect: "/intelligence/sources",
    meta: { title: "漏洞情报收录", icon: "InfoFilled" },
    children: [
      {
        path: "sources",
        name: "IntelligenceSources",
        component: () => import("@/views/intelligence/Sources.vue"),
        meta: { title: "多源情报收录", icon: "Collection" },
      },
      // {
      //   path: "manual",
      //   name: "ManualImport",
      //   component: () => import("@/views/intelligence/Manual.vue"),
      //   meta: { title: "手动上传导入", icon: "Upload" },
      // },
    ],
  },
  // 漏洞验证环境管理
  {
    path: "/verification-env",
    component: Layout,
    redirect: "/verification-env/list",
    meta: { title: "漏洞验证环境管理", icon: "Monitor" },
    children: [
      {
        path: "list",
        name: "EnvironmentList",
        component: () => import("@/views/verification-env/List.vue"),
        meta: { title: "漏洞验证环境", icon: "List" },
      },
      {
        path: "manage",
        name: "ScenesManage",
        component: () => import("@/views/verification-env/index.vue"),
        meta: {
          defaultMenu: false,
          hidden: true,
          title: "场景详情",
          icon: "Changjingguanli",
        },
      },
      // {
      //   path: "taskList",
      //   name: "TaskList",
      //   component: () => import("@/views/verification-env/List.vue"),
      //   meta: { title: "漏洞验证环境列表", icon: "List" },
      // },
      // {
      //   path: "config",
      //   name: "EnvironmentConfig",
      //   component: () => import("@/views/verification-env/Config.vue"),
      //   meta: { title: "环境配置", icon: "Setting" },
      // },
      {
        path: "monitor",
        name: "ProcessMonitor",
        component: () => import("@/views/verification-env/Monitor.vue"),
        meta: { title: "漏洞验证", icon: "View", hidden: true },
      },
    ],
  },
  // 漏洞验证记录
  {
    path: "/verification-record",
    component: Layout,
    redirect: "/verification-record/list",
    meta: { title: "漏洞验证任务管理", icon: "Document" },
    children: [
      {
        path: "list",
        name: "RecordList",
        component: () => import("@/views/verification-record/List.vue"),
        meta: { title: "漏洞验证任务", icon: "List" },
      },
    // {
    //   path: "template",
    //   name: "RecordTemplate",
    //   component: () => import("@/views/verification-record/Template.vue"),
    //   meta: { title: "文档模板管理", icon: "Document" },
    // },
    // {
    //   path: "rules",
    //   name: "RecordRules",
    //   component: () => import("@/views/verification-record/Rules.vue"),
    //   meta: { title: "文档访问控制", icon: "Lock" },
    // },
    ],
  },
  // 访问控制与权限管理
  // {
  //   path: "/access-control",
  //   component: Layout,
  //   redirect: "/access-control/users",
  //   meta: { title: "访问控制与权限管理", icon: "Lock" },
  //   children: [
  //     {
  //       path: "operation-logs",
  //       name: "OperationLogs",
  //       component: () => import("@/views/access-control/OperationLogs.vue"),
  //       meta: { title: "操作审计", icon: "Notebook" },
  //     },
  //   ],
  // },
  {
    path: "/vulnFile",
    component: Layout,
    redirect: "/vulnFile/list",
    meta: { title: "漏洞文档管理", icon: "User" },
    children: [
      {
        path: "list",
        name: "VulnFileList",
        component: () => import("@/views/vulnFile/List.vue"),
        meta: { title: "漏洞文档管理", icon: "List" },
      }
    ]
  },
  // 用户管理
  {
    path: "/access-control",
    component: Layout,
    redirect: "/access-control/list",
    meta: { title: "用户管理", icon: "User" },
    children: [
      {
        path: "list",
        name: "UserManageList",
        component: () => import("@/views/access-control/List.vue"),
        meta: { title: "用户管理列表", icon: "List" },
      }
    ]
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

/**
 * @description 路由守卫
 */
router.beforeEach(async (to, from, next) => {
  NProgress.start();

  // 设置页面标题
  document.title = to.meta.title
    ? `${to.meta.title} - 漏洞管理系统`
    : "漏洞管理系统";

  const token = localStorage.getItem("token");

  // if (token) {
  //   if (to.path === '/login') {
  //     next('/')
  //     NProgress.done()
  //   } else {
  //     // 这里可以添加获取用户信息和动态路由的逻辑
  //     next()
  //   }
  // } else {
  //   if (to.path === '/login') {
  //     next()
  //   } else {
  //     next(`/login?redirect=${to.path}`)
  //     NProgress.done()
  //   }
  // }
  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
